﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace iLangl.Model
{
	public class PluginRegistrationResponseContext : BaseResponseContext
	{
		PluginRegistration registration = null;

		public override void SetContext(object context)
		{
			base.SetContext(context);

			if (document == null)
				return;

			XmlNode nodepluginRegistration = document.SelectSingleNode("//PluginRegistration");

			if (nodepluginRegistration == null)
				return;

			ParseExtendedInfo(nodepluginRegistration);

			registration = new PluginRegistration();

			if (nodepluginRegistration.Attributes["pluginId"] != null)
			{
				registration.PluginId = nodepluginRegistration.Attributes["pluginId"].Value;
			}

			if (nodepluginRegistration.Attributes["pluginName"] != null)
			{
				registration.PluginName = nodepluginRegistration.Attributes["pluginName"].Value;
			}
			
			if (nodepluginRegistration.Attributes["pluginVersion"] != null)
			{
				registration.PluginVersion = nodepluginRegistration.Attributes["pluginVersion"].Value;
			}

			if (nodepluginRegistration.Attributes["licenseOwner"] != null)
			{
				registration.PrimaryContactId = nodepluginRegistration.Attributes["licenseOwner"].Value;
			}

			if (nodepluginRegistration.Attributes["sessionId"] != null)
			{
				registration.SessionId = nodepluginRegistration.Attributes["sessionId"].Value;
			}

			if (nodepluginRegistration.Attributes["pushURI"] != null)
			{
				registration.PushURIAddress = nodepluginRegistration.Attributes["pushURI"].Value;
			}

			CompanyResponseContext companyResponseContext = new CompanyResponseContext();
			companyResponseContext.SetContext(nodepluginRegistration);
			registration.Company = (Company)companyResponseContext.GetContext();
		}

		public override object GetContext()
		{
			return registration;
		}
	}
}
